/*
 * @Description:
 * @Autor: liujinyin
 * @Date: 2021-06-28 18:40:43
 * @LastEditors: liujinyin
 * @LastEditTime: 2021-07-02 19:58:33
 */
package secret

import (
	"strings"

	"gitee.com/micro-plat/sas/sas/modules/const/enum"
	"gitee.com/micro-plat/sas/sas/modules/const/models"
)

//ISecret 接口
type ISecret interface {
	Generate(euid, sType string, item *models.Item) (interface{}, error)
	Get(euid string, sType ...string) (interface{}, error)
	Reset(euid, sType string, item *models.Item) (res interface{}, err error)
	Enable(euid, sType string) (res interface{}, err error)
	Disable(euid, sType string) (res interface{}, err error)
	Set(euid, sType string, item *models.Item) (res interface{}, err error)
}

//Secret 对象
type Secret struct {
	general *GeneralSecret
	rsa     *RSASecret
	cert    *CertSecret
}

//NewSecret 创建接口
func NewSecret() *Secret {
	return &Secret{
		general: NewGeneralSecret(),
		rsa:     NewRSASecret(),
		cert:    NewCertSecret(),
	}
}

//Generate 生成
func (s *Secret) Generate(euid, sType string, item *models.Item) (interface{}, error) {
	if strings.EqualFold(sType, enum.RSAStr) {
		return s.rsa.Generate(euid, item)
	}
	if strings.EqualFold(sType, enum.CertStr) {
		return s.cert.Generate(euid, item)
	}
	return s.general.Generate(euid, sType)
}

//Get 获取
func (s *Secret) Get(euid string, sType ...string) (interface{}, error) {
	return s.general.Get(euid, sType...)
}

//Reset 重置
func (s *Secret) Reset(euid, sType string, item *models.Item) (res interface{}, err error) {

	if strings.EqualFold(sType, enum.RSAStr) {
		return s.rsa.Reset(euid, item)
	}
	if strings.EqualFold(sType, enum.CertStr) {
		return s.cert.Reset(euid, item)
	}
	return s.general.Reset(euid, sType, item)
}

//Enable 启用
func (s *Secret) Enable(euid, sType string) (res interface{}, err error) {
	if strings.EqualFold(sType, enum.RSAStr) {
		return s.rsa.Enable(euid)
	}
	if strings.EqualFold(sType, enum.CertStr) {
		return s.cert.Enable(euid)
	}
	return s.general.Enable(euid, sType)
}

//Disable 禁用
func (s *Secret) Disable(euid, sType string) (res interface{}, err error) {
	if strings.EqualFold(sType, enum.RSAStr) {
		return s.rsa.Disable(euid)
	}
	if strings.EqualFold(sType, enum.CertStr) {
		return s.cert.Disable(euid)
	}
	return s.general.Disable(euid, sType)
}

//Set 设置秘钥

func (s *Secret) Set(euid, sType string, item *models.Item) (res interface{}, err error) {

	if strings.EqualFold(sType, enum.RSAStr) {
		return s.rsa.Set(euid, item)
	}
	if strings.EqualFold(sType, enum.CertStr) {
		return s.cert.Set(euid, item)
	}
	return s.general.Set(euid, sType, item.GetSecret(enum.GetNameByType(sType)), item.ExpireTime)
}
